<!--
  智慧社区管理系统 - 紧急呼叫管理页面
  @author Wu.Liang
  @date 2025-01-20
  @description 紧急呼叫处理、救援调度管理
-->
<template>
  <div class="emergency-call-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <el-icon size="24" class="title-icon"><Phone /></el-icon>
          <h2>紧急呼叫管理</h2>
        </div>
        <div class="header-actions">
          <el-button type="danger" @click="handleEmergencyCall">
            <el-icon><Phone /></el-icon>
            紧急呼叫
          </el-button>
          <el-button @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card total-calls">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Phone /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.totalCalls }}</div>
                <div class="stat-label">呼叫总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card emergency-calls">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Warning /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.emergencyCalls }}</div>
                <div class="stat-label">紧急呼叫</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card processing-calls">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.processingCalls }}</div>
                <div class="stat-label">处理中</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card resolved-calls">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Check /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.resolvedCalls }}</div>
                <div class="stat-label">已解决</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="呼叫类型">
          <el-select
            v-model="searchForm.callType"
            placeholder="请选择呼叫类型"
            clearable
            style="width: 150px"
          >
            <el-option label="紧急求助" :value="1" />
            <el-option label="医疗救助" :value="2" />
            <el-option label="火灾报警" :value="3" />
            <el-option label="治安事件" :value="4" />
            <el-option label="其他" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select
            v-model="searchForm.handleStatus"
            placeholder="请选择处理状态"
            clearable
            style="width: 150px"
          >
            <el-option label="待处理" :value="0" />
            <el-option label="处理中" :value="1" />
            <el-option label="已解决" :value="2" />
            <el-option label="已关闭" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="呼叫人">
          <el-input
            v-model="searchForm.callerName"
            placeholder="请输入呼叫人姓名"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="呼叫时间">
          <el-date-picker
            v-model="searchForm.callTimeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 350px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 呼叫列表 -->
    <el-card class="call-list-card">
      <template #header>
        <div class="card-header">
          <span>紧急呼叫列表</span>
          <div class="header-actions">
            <el-button type="success" size="small" @click="handleBatchProcess">
              <el-icon><Check /></el-icon>
              批量处理
            </el-button>
            <el-button type="warning" size="small" @click="handleBatchAssign">
              <el-icon><User /></el-icon>
              批量分配
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="callList"
        v-loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="callType" label="呼叫类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getCallTypeTag(row.callType)">
              {{ getCallTypeName(row.callType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="callerName" label="呼叫人" width="120" />
        <el-table-column prop="callerPhone" label="联系电话" width="130" />
        <el-table-column prop="location" label="事发地点" min-width="150" />
        <el-table-column prop="description" label="事件描述" min-width="200" />
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag :type="getPriorityTag(row.priority)">
              {{ getPriorityName(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="handleStatus" label="处理状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getHandleStatusTag(row.handleStatus)">
              {{ getHandleStatusName(row.handleStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="callTime" label="呼叫时间" width="180" />
        <el-table-column prop="handleTime" label="处理时间" width="180" />
        <el-table-column prop="handler" label="处理人" width="120" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleProcess(row)" v-if="row.handleStatus === 0">
              <el-icon><Check /></el-icon>
              处理
            </el-button>
            <el-button type="warning" size="small" @click="handleAssign(row)" v-if="row.handleStatus === 0">
              <el-icon><User /></el-icon>
              分配
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 呼叫详情弹窗 -->
    <el-dialog
      v-model="callDetailVisible"
      title="紧急呼叫详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="呼叫类型">{{ getCallTypeName(currentCall.callType) }}</el-descriptions-item>
        <el-descriptions-item label="呼叫人">{{ currentCall.callerName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentCall.callerPhone }}</el-descriptions-item>
        <el-descriptions-item label="事发地点">{{ currentCall.location }}</el-descriptions-item>
        <el-descriptions-item label="优先级">
          <el-tag :type="getPriorityTag(currentCall.priority)">
            {{ getPriorityName(currentCall.priority) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag :type="getHandleStatusTag(currentCall.handleStatus)">
            {{ getHandleStatusName(currentCall.handleStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="呼叫时间">{{ currentCall.callTime }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ currentCall.handleTime || '未处理' }}</el-descriptions-item>
        <el-descriptions-item label="处理人">{{ currentCall.handler || '未分配' }}</el-descriptions-item>
        <el-descriptions-item label="响应时间">{{ currentCall.responseTime || '未响应' }}</el-descriptions-item>
        <el-descriptions-item label="事件描述" :span="2">{{ currentCall.description }}</el-descriptions-item>
        <el-descriptions-item label="处理结果" :span="2">{{ currentCall.result || '暂无结果' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentCall.remark || '暂无备注' }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <el-button @click="callDetailVisible = false">关闭</el-button>
        <el-button type="success" @click="handleProcess(currentCall)" v-if="currentCall.handleStatus === 0">处理呼叫</el-button>
        <el-button type="warning" @click="handleAssign(currentCall)" v-if="currentCall.handleStatus === 0">分配处理</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  Phone,
  Refresh,
  Warning,
  Clock,
  Check,
  Search,
  User,
  View
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const callDetailVisible = ref(false)
const selectedCalls = ref<any[]>([])

// 搜索表单
const searchForm = reactive({
  callType: undefined as number | undefined,
  handleStatus: undefined as number | undefined,
  callerName: '',
  callTimeRange: [] as string[]
})

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

// 统计数据
const statistics = reactive({
  totalCalls: 89,
  emergencyCalls: 23,
  processingCalls: 8,
  resolvedCalls: 58
})

// 当前呼叫详情
const currentCall = ref<any>({})

// 呼叫列表数据
const callList = ref([
  {
    id: 1,
    callType: 1,
    callerName: '张三',
    callerPhone: '13800138001',
    location: '1号楼101室',
    description: '老人突发心脏病，需要紧急医疗救助',
    priority: 3,
    handleStatus: 1,
    callTime: '2025-01-20 15:30:25',
    handleTime: '2025-01-20 15:32:10',
    handler: '李医生',
    responseTime: '2025-01-20 15:31:00',
    result: '已联系120急救，医生正在赶往现场',
    remark: '紧急医疗救助'
  },
  {
    id: 2,
    callType: 3,
    callerName: '王五',
    callerPhone: '13800138002',
    location: '地下车库',
    description: '发现烟雾，疑似火灾',
    priority: 3,
    handleStatus: 0,
    callTime: '2025-01-20 16:15:30',
    handleTime: null,
    handler: null,
    responseTime: null,
    result: null,
    remark: '火灾报警'
  },
  {
    id: 3,
    callType: 4,
    callerName: '赵六',
    callerPhone: '13800138003',
    location: '小区前门',
    description: '发现可疑人员徘徊，需要安保人员查看',
    priority: 2,
    handleStatus: 2,
    callTime: '2025-01-20 14:20:15',
    handleTime: '2025-01-20 14:25:30',
    handler: '保安小王',
    responseTime: '2025-01-20 14:22:00',
    result: '已驱散可疑人员，加强巡逻',
    remark: '治安事件'
  },
  {
    id: 4,
    callType: 2,
    callerName: '孙七',
    callerPhone: '13800138004',
    location: '2号楼203室',
    description: '孩子发烧，需要医疗咨询',
    priority: 2,
    handleStatus: 2,
    callTime: '2025-01-20 17:00:45',
    handleTime: '2025-01-20 17:05:20',
    handler: '社区医生',
    responseTime: '2025-01-20 17:02:30',
    result: '已提供医疗建议，建议及时就医',
    remark: '医疗咨询'
  },
  {
    id: 5,
    callType: 5,
    callerName: '周八',
    callerPhone: '13800138005',
    location: '3号楼305室',
    description: '水管爆裂，需要紧急维修',
    priority: 2,
    handleStatus: 1,
    callTime: '2025-01-20 18:30:10',
    handleTime: '2025-01-20 18:35:00',
    handler: '维修师傅',
    responseTime: '2025-01-20 18:32:00',
    result: '正在维修中',
    remark: '紧急维修'
  }
])

// 获取呼叫类型标签样式
const getCallTypeTag = (type: number): 'danger' | 'warning' | 'info' | 'success' => {
  const typeMap: Record<number, 'danger' | 'warning' | 'info' | 'success'> = {
    1: 'danger',   // 紧急求助
    2: 'warning',  // 医疗救助
    3: 'danger',   // 火灾报警
    4: 'warning',  // 治安事件
    5: 'info'      // 其他
  }
  return typeMap[type] || 'info'
}

// 获取呼叫类型名称
const getCallTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '紧急求助',
    2: '医疗救助',
    3: '火灾报警',
    4: '治安事件',
    5: '其他'
  }
  return typeMap[type] || '未知类型'
}

// 获取优先级标签样式
const getPriorityTag = (priority: number): 'info' | 'warning' | 'danger' => {
  const priorityMap: Record<number, 'info' | 'warning' | 'danger'> = {
    1: 'info',     // 低
    2: 'warning',  // 中
    3: 'danger'    // 高
  }
  return priorityMap[priority] || 'info'
}

// 获取优先级名称
const getPriorityName = (priority: number): string => {
  const priorityMap: Record<number, string> = {
    1: '低',
    2: '中',
    3: '高'
  }
  return priorityMap[priority] || '未知'
}

// 获取处理状态标签样式
const getHandleStatusTag = (status: number): 'danger' | 'warning' | 'success' | 'info' => {
  const statusMap: Record<number, 'danger' | 'warning' | 'success' | 'info'> = {
    0: 'danger',   // 待处理
    1: 'warning',  // 处理中
    2: 'success',  // 已解决
    3: 'info'      // 已关闭
  }
  return statusMap[status] || 'danger'
}

// 获取处理状态名称
const getHandleStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: '待处理',
    1: '处理中',
    2: '已解决',
    3: '已关闭'
  }
  return statusMap[status] || '未知状态'
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadCallList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    callType: undefined,
    handleStatus: undefined,
    callerName: '',
    callTimeRange: []
  })
  handleSearch()
}

// 加载呼叫列表
const loadCallList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    pagination.total = callList.value.length
  }, 500)
}

// 紧急呼叫
const handleEmergencyCall = () => {
  showMessage({ type: 'info', message: '紧急呼叫功能开发中' })
}

// 刷新
const handleRefresh = () => {
  loadCallList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

// 查看呼叫详情
const handleView = (row: any) => {
  currentCall.value = { ...row }
  callDetailVisible.value = true
}

// 处理呼叫
const handleProcess = (row: any) => {
  showMessage({ type: 'info', message: '处理呼叫功能开发中' })
}

// 分配处理
const handleAssign = (row: any) => {
  showMessage({ type: 'info', message: '分配处理功能开发中' })
}

// 批量处理
const handleBatchProcess = () => {
  if (selectedCalls.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要处理的呼叫' })
    return
  }
  showMessage({ type: 'info', message: '批量处理功能开发中' })
}

// 批量分配
const handleBatchAssign = () => {
  if (selectedCalls.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要分配的呼叫' })
    return
  }
  showMessage({ type: 'info', message: '批量分配功能开发中' })
}

// 选择变化处理
const handleSelectionChange = (selection: any[]) => {
  selectedCalls.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadCallList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  loadCallList()
}

// 页面初始化
onMounted(() => {
  loadCallList()
})
</script>

<style scoped lang="scss">
.emergency-call-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-title {
        display: flex;
        align-items: center;
        color: white;

        .title-icon {
          margin-right: 12px;
        }

        h2 {
          margin: 0;
          font-size: 24px;
          font-weight: 600;
        }
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }

  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 8px;

        .stat-icon {
          margin-right: 16px;
          padding: 12px;
          border-radius: 8px;
          color: white;
        }

        .stat-info {
          .stat-number {
            font-size: 28px;
            font-weight: bold;
            line-height: 1;
            margin-bottom: 4px;
          }

          .stat-label {
            font-size: 14px;
            color: #666;
          }
        }
      }

      &.total-calls .stat-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      &.emergency-calls .stat-icon {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
      }

      &.processing-calls .stat-icon {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      &.resolved-calls .stat-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }
    }
  }

  .search-card {
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }

  .call-list-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
      font-size: 16px;

      .header-actions {
        display: flex;
        gap: 8px;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;
    }
  }
}
</style> 